﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WhittleList.Bll;

namespace WhittleList.Controllers
{
    public class BaseController : Controller
    {
        public IUserRepository UserRepository { get; private set; }
        public ISiteRepository SiteRepository { get; private set; }
        
        protected BaseController(IUserRepository userRepository, ISiteRepository siteRepository)
        {
            UserRepository = userRepository;
            SiteRepository = siteRepository;
        }

        private Bll.User _CurrentUser;
        public Bll.User CurrentUser
        {
            get
            {
                if(_CurrentUser == null && base.User.Identity != null)
                {
                    _CurrentUser = UserRepository.GetByEmailAddress(base.User.Identity.Name);
                }
                return _CurrentUser;
            }
        }

        private Bll.Site _CurrentSite;
        public Bll.Site CurrentSite
        {
            get
            {
                if (_CurrentSite == null)
                {
                    //split the url
                    string host = HttpContext.Request.Headers["HOST"].Split(':')[0];

                    var rep = new Bll.SiteRepository();
                    _CurrentSite = rep.Site_GetByUrl(host);
                }
                return _CurrentSite;
            }
        }

        public bool IsMainSite()
        {
            var url = HttpContext.Request.Headers["HOST"].Split(':')[0];
            return url == "www.whittledev.com" || url == "localhost";
        }
    }
}
